
import 'dart:io';

import 'package:bigegg/common/log/xmm_logcat.dart';
import 'package:bigegg/utils/xmm_toast_util.dart';
import 'package:file_picker/file_picker.dart';
import 'package:image_picker/image_picker.dart';
import 'package:permission_handler/permission_handler.dart';

class XmmMediaUtils {
  static Future<String?> pickImage() async {
    Permission permission = Permission.photos;
    PermissionStatus status = await permission.request();
    if (!status.isGranted) {
      XmmToastUtils.show("You have not authorised this permission.");
      return null;
    }
    XFile? image = await ImagePicker().pickImage(source: ImageSource.gallery);
    if (image == null) {
      XmmXLogcat.e("pickImage null error");
      return null;
    }
    return image.path;
  }

  //选择本地文件，成功返回文件信息
  static Future<File?> pickFile() async {
    PermissionStatus status = await Permission.storage.request();
    if (status != PermissionStatus.granted) {
      XmmToastUtils.show("You have not authorised this permission.");
      return null;
    }
    FilePickerResult? result = await FilePicker.platform.pickFiles();
    if (result != null) {
      return File(result.files.single.path!);
    } else {
      return null;
    }
  }

  static Future<String?> takePhoto() async {
    PermissionStatus status  = await Permission.camera.request();
    if(status != PermissionStatus.granted) {
      XmmToastUtils.show("You have not authorised this permission.");
      return null;
    }
    XFile? image = await ImagePicker().pickImage(source: ImageSource.camera);
    if (image == null) {
      XmmXLogcat.e("pickImage null error");
      return null;
    }
    return image.path;
  }
}
